package com.example.scheduleendpoint.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class ScheduleService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleService.class);

    @Scheduled(initialDelay = 1000 * 1, fixedDelay = 1000 * 10)
    public void schedule1(){
        LOGGER.warn("schedule1 begin");
        randomSleep();
        LOGGER.warn("schedule1 end");
    }

    @Scheduled(initialDelay = 1000 * 2, fixedDelay = 1000 * 20)
    public void schedule2(){
        LOGGER.warn("schedule2 begin");
        randomSleep();
        LOGGER.warn("schedule2 end");
    }

    @Scheduled(initialDelay = 1000 * 3, fixedDelay = 1000 * 30)
    public void schedule3(){
        LOGGER.warn("schedule3 begin");
        randomSleep();
        LOGGER.warn("schedule3 end");
    }

    @Scheduled(initialDelay = 1000 * 4, fixedDelay = 1000 * 60)
    public void schedule4(){
        LOGGER.warn("schedule4 begin");
        randomSleep();
        LOGGER.warn("schedule4 end");
    }

    @Scheduled(initialDelay = 1000 * 5, fixedDelay = 1000 * 120)
    public void schedule5(){
        LOGGER.warn("schedule5 begin");
        randomSleep();
        LOGGER.warn("schedule5 end");
    }


    private void randomSleep(){
        Random random = new Random();
        int ms = Math.abs(random.nextInt() % 10000);
        try {
            TimeUnit.MILLISECONDS.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
